package com.smedi.ismedi.personnel.core.application.service.impl;

import com.smedi.ismedi.personnel.core.application.service.ScheduleApplicationService;
import com.smedi.ismedi.personnel.core.infrastructure.schedule.DoImisSyncTask;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Service;

import java.util.concurrent.ScheduledFuture;

/**
 * @Author: xulinjun
 * @Date: 2021-6-16 17:00
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ScheduleApplicationServiceImpl implements ScheduleApplicationService {
    @Value("${schedule.corn.doSync}")
    private String doSyncCorn;

    private final ThreadPoolTaskScheduler threadPoolTaskScheduler;

    private final DoImisSyncTask doImisSyncTask;

    private ScheduledFuture<?> doSync;

    @Override
    public void runSync() {
        if (doSync == null) {
            doSync = threadPoolTaskScheduler.schedule(doImisSyncTask, triggerContext -> new CronTrigger(doSyncCorn).nextExecutionTime(triggerContext));
            log.info(">>> 开启定期定期同步人员任务！");
        }
    }

    @Override
    public void stopSync() {
        if (doSync != null) {
            doSync.cancel(true);
            doSync = null;
            log.info(">>> 关闭定期定期同步人员任务!");
        }
    }
}
